<script setup>
import { ref } from 'vue'
import { CourseGetListService } from '@/api/Course.js'
import PageContainer from '@/components/PageContainer.vue'
import { Avatar } from '@element-plus/icons-vue'
import { userSelectCourseService } from '@/api/user.js'

const CourseList = ref([])

const total = ref(10)

const formModel = ref({
  id: '',
  className: '',
  teacher: '',
  room: '',
  capacity: ''
})

const onSelectCourse = async (row) => {
  console.log(row)
  formModel.value = row
  await userSelectCourseService(formModel.value)
  alert("选择课程成功")
  getCourseList()
}

const onSearch = () => {
  params.value.page = 1
  getCourseList()
}

const onReset = () => {
  params.value.page = 1
  params.value.className = ''
  getCourseList()
}

const onSizeChange = (size) => {
  params.value.page = 1
  params.value.pageSize = size
  getCourseList()
}
const onCurrentChange = (page) => {
  params.value.page = page
  getCourseList()
}

const params = ref({
  page: 1,
  pageSize: 6,
  className: ''
})


const getCourseList = async () => {
  const res = await CourseGetListService(params.value)
  CourseList.value = res.data.data.rows
  total.value = res.data.data.total
}
getCourseList()
</script>

<template>
  <page-container title="可选课程查看">
    <el-form inline ref="formRef">
      <span>课程名称：</span>
      <el-input
        v-model="params.className"
        placeholder="请输入课程名称"
        style="margin: 20px; width: 200px"
      ></el-input>
      <el-form-item>
        <el-button type="primary" @click="onSearch">搜索</el-button>
        <el-button @click="onReset">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="CourseList" style="width: 100%">
      <el-table-column label="课程编号" width="300" prop="id"></el-table-column>
      <el-table-column
        label="课程名称"
        width="300"
        prop="className"
      ></el-table-column>
      <el-table-column
        label="授课老师"
        width="300"
        prop="teacher"
      ></el-table-column>
      <el-table-column
        label="授课地点"
        width="300"
        prop="room"
      ></el-table-column>
      <el-table-column label="课堂容量" prop="capacity"></el-table-column>
      <el-table-column label="操作" width="100">

        <template #default="{ row }">
          <el-button
            :icon="Avatar"
            circle
            plain
            type="primary"
            @click="onSelectCourse(row)"
          ></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>

    <el-pagination
      v-model:current-page="params.pagenum"
      v-model:page-size="params.pagesize"
      :page-sizes="[2, 3, 5, 10]"
      :background="true"
      layout="jumper, total, sizes, prev, pager, next"
      :total="total"
      @size-change="onSizeChange"
      @current-change="onCurrentChange"
      style="margin-top: 20px; justify-content: flex-end"
    />
  </page-container>
</template>

<style lang="scss" scoped></style>
